package br.ujr.pocs.activemq.queue;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ConsumerQueue {
	
	public static void main(String[] args) {
		String url = "tcp://localhost:61616";
    	Connection connection = null;
    	Session session = null;
    	
    	try {
    		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    		connection = factory.createConnection();
    		connection.start();
    		
    		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		Destination destination = session.createQueue("TEST.FOO");
    		MessageConsumer consumer = session.createConsumer(destination);
    		
    		Message message = consumer.receive();
    		TextMessage textMessage = (TextMessage) message;
    		System.out.printf("Mensagem \"%s\" consumida.", textMessage.getText());
    		
    		
    	} catch (JMSException e) {
    		throw new RuntimeException(e);
    	} finally {
    		try {
    			if ( connection != null ) connection.close();
    		} catch (JMSException e) {
    			
    			throw new RuntimeException(e);
    		}
    	}
		
		
		
	}
}
